home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / open.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  2KB  |  116 lines

  1. /* based upon Dale Schumacher's dLibs library */
  2. /* extensively modified by ers */
  3.  
  4. #define __SRC__
  5. #include <osbind.h>
  6. #include <mintbind.h>
  7. #include <limits.h>
  8. #include <fcntl.h>
  9. #include <ioctl.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include "lib.h"
  13.  
  14. static int __umask = -1;
  15. extern int __mint;
  16.  
  17. /*
  18.  * function to set the initial value of __umask
  19.  */
  20.  
  21. static void
  22. _get_umask()
  23. {
  24.     if (__mint < 9) {
  25.         __umask = 0;
  26.     } else {
  27.         __umask = Pumask(0);
  28.         (void) Pumask(__umask);
  29.     }
  30. }
  31.  
  32. int open(_filename, iomode, pmode)
  33.     const char *_filename;
  34.     int iomode;
  35.     unsigned pmode;
  36. {
  37.     int rv;
  38.     int modemask;            /* which bits get passed to the OS? */
  39.     char filename[PATH_MAX];
  40.  
  41.     _unx2dos(_filename, filename);
  42.  
  43. /* use the umask() setting to get the right permissions */
  44.     if (__umask == -1)
  45.         _get_umask();
  46.     pmode &= ~__umask;
  47.  
  48. /* set the file access modes correctly */
  49.     iomode = iomode & ~O_SHMODE;
  50.  
  51.     if (__mint >= 9) {
  52.         modemask = O_ACCMODE | O_SHMODE | O_SYNC | O_NDELAY;
  53.         iomode |= (iomode & O_EXCL) ? O_DENYRW : O_DENYNONE;
  54.     } else {
  55.         modemask = O_ACCMODE;
  56.     }
  57.  
  58.     if(Fattrib(filename, 0, 0) >= 0)        /* file exists */
  59.     {
  60.         if((iomode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
  61.             errno = EEXIST;
  62.             return -1;
  63.         }
  64.         if(iomode & O_TRUNC)
  65.             rv = Fcreate(filename, 0x00);
  66.         else
  67.             rv = Fopen(filename,iomode & modemask);
  68.     }
  69.     else                    /* file doesn't exist */
  70.     {
  71.         if(iomode & O_CREAT) {
  72.             rv = Fcreate(filename, 0x00);
  73.             if (rv >= 0 && __mint >= 9)
  74.                 (void)Fchmod(filename, pmode);
  75.         }
  76.         else
  77.             rv = -ENOENT;
  78.     }
  79.  
  80.     if((rv >= 0) && (iomode & O_APPEND))
  81.         (void)lseek(rv, 0L, SEEK_END);
  82.  
  83.     if(rv < (__SMALLEST_VALID_HANDLE)) {
  84.         errno = -rv;
  85.         return __SMALLEST_VALID_HANDLE - 1;
  86.     }
  87.  
  88.     (void)isatty(rv);    /* sets up tty flags under TOS */
  89.  
  90.     return(rv);
  91. }
  92.  
  93. int
  94. creat(name, mode)
  95.     const char *name;
  96.     unsigned   mode;
  97. {
  98.     return open(name, O_WRONLY|O_CREAT|O_TRUNC, mode);
  99. }
  100.  
  101. /* umask -- change default file creation mask */
  102.  
  103. int umask(complmode)
  104.        int complmode;
  105. {
  106.     int old_umask;
  107.  
  108.     if (__umask == -1)
  109.         _get_umask();
  110.     old_umask = __umask;
  111.     __umask = complmode;
  112.     if (__mint >= 9)
  113.         return Pumask(complmode);
  114.     return old_umask;
  115. }
  116.